package com.dd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.dto.CategoryDTO;
import com.dd.entity.Category;
import com.dd.exception.BusinessException;
import com.dd.mapper.CategoryMapper;
import com.dd.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 菜品分类表 服务实现类
 * </p>
 *
 * @author dhd
 * @since 2025-01-09 06:56
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    @Transactional
    public Category create(CategoryDTO categoryDTO) {
        // 检查分类名是否已存在
        if (isNameExists(categoryDTO.getName())) {
            throw new BusinessException("分类名称已存在");
        }

        Category category = new Category();
        category.setName(categoryDTO.getName());
        category.setDescription(categoryDTO.getDescription());

        categoryMapper.insert(category);
        return category;
    }

    @Override
    @Transactional
    public Category update(Long id, CategoryDTO categoryDTO) {
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            throw new BusinessException("分类不存在");
        }

        // 如果名称变更，检查新名称是否已存在
        if (!category.getName().equals(categoryDTO.getName()) && isNameExists(categoryDTO.getName())) {
            throw new BusinessException("分类名称已存在");
        }

        category.setName(categoryDTO.getName());
        category.setDescription(categoryDTO.getDescription());

        categoryMapper.updateById(category);
        return category;
    }

    @Override
    @Transactional
    public void delete(Long id) {
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            throw new BusinessException("分类不存在");
        }

        // TODO: 检查分类下是否有菜品，如果有则不允许删除

        categoryMapper.deleteById(id);
    }

    @Override
    public Category getById(Long id) {
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            throw new BusinessException("分类不存在");
        }
        return category;
    }

    @Override
    public Page<Category> list(int page, int size, String name) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>()
                // 如果name不为空，添加模糊查询条件
                .like(StringUtils.hasText(name), Category::getName, name)
                .orderByDesc(Category::getCreatedAt);

        return categoryMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    private boolean isNameExists(String name) {
        return categoryMapper.selectCount(
                new LambdaQueryWrapper<Category>()
                        .eq(Category::getName, name)
        ) > 0;
    }
}
